import gtk


class MyApp(object):
    def __init__(self):
        self.builder = gtk.Builder()
        self.builder.add_from_file("beispiel2.ui")
        self.builder.connect_signals(self)

        self.win =  self.builder.get_object('window1')
        self.path = None

    def run(self):
        try:
            gtk.main()
        except KeyboardInterrupt:
            pass
    
    def quit(self):
        gtk.main_quit()


    def ask_for_filename(self, title, default=None):
        dlg = gtk.FileChooserDialog(title=title,
                                    parent=self.win, 
                                    buttons=(gtk.STOCK_CANCEL,
                                             gtk.RESPONSE_REJECT,
                                             gtk.STOCK_OK,
                                             gtk.RESPONSE_OK))

        if default is not None:
            dlg.set_filename(default)

        result = dlg.run()

        if result == gtk.RESPONSE_OK:
            path = dlg.get_filename()
        else:
            path = None

        dlg.destroy()
        return path


    def info_msg(self, msg):
        dlg = gtk.MessageDialog(parent=self.win, 
                                type=gtk.MESSAGE_INFO, 
                                buttons=gtk.BUTTONS_OK,
                                message_format=msg
                                )
        dlg.run()
        dlg.destroy()


#####################
## Signal-Behandlung
#####################

###############
## Hauptfenster

    def on_window1_delete_event(self, *args):
        self.quit()

###########
## Aktionen

    def on_act_quit_activate(self, *args):
        self.quit()

    def on_act_info_activate(self, *args):
        self.info_msg('Info')

    def on_act_delete_activate(self, *args): 
        self.info_msg('Löschen') 

    def on_act_paste_activate(self, *args):
        self.info_msg('Einfügen') 

    def on_act_copy_activate(self, *args):
        self.info_msg('Kopieren') 

    def on_act_cut_activate(self, *args):
        self.info_msg('Ausschneiden') 

    def on_act_save_activate(self, *args):
        self.info_msg('Speichern') 

    def on_act_new_activate(self, *args):
        self.info_msg('Neu') 
 
    def on_act_save_as_activate(self, *args):
        path = self.ask_for_filename('Datei öffnen', self.path)
        if path is None:
            self.info_msg('Datei nicht gespeichert')
        else:
            self.path = path
            self.info_msg('Datei "%s" gespeichert.'%self.path) 

    def on_act_open_activate(self, *args):
        path = self.ask_for_filename('Datei öffnen', self.path)
        if path is None:
            self.info_msg('Keine Datei ausgewählt')
        else:
            self.path = path
            self.info_msg('Datei "%s" ausgewählt.'%self.path)


if __name__ == '__main__':
    app = MyApp()
    app.run()
